<?php

session_name( 'SESS_SITEMANAGE' );
session_start();

 if( isset($_SESSION['ADMIN_LOGIN']['STAFF_ID']) && $_SESSION['ADMIN_LOGIN']['STAFF_ID'] ){
 	  header('Location: ../menu.php');
      exit;
 }
require_once('../../core.inc.php');
require_once('Model/SiteManage.php');
require_once('View/Smarty.php');


//
$sys    = new Model_SiteManage();
$params = $_REQUEST;
$smarty = new View_Smarty($params);
//

if (isset($params['do']) && function_exists('exec_' . $params['do'])){
    $params['op'] = call_user_func('exec_' . $params['do'], &$params, &$smarty, &$sys);
}
if (isset($params['op']) && function_exists('op_' . $params['op'])){
    call_user_func('op_' . $params['op'], &$params, &$smarty, &$sys);
}
else{
    call_user_func('op_login', &$params, &$smarty, &$sys);
}
//
function op_login(&$params, &$smarty, &$sys)
{
    if (!isset($params['data'])){
        $data               = array();
        $data['email']      = '';
        $data['login_pass'] = '';
        $data['keep_login'] = '';
        $params['data']     = $data;
    }
    else{
        if (!isset($params['data']['keep_login'])){
            $params['data']['keep_login'] = '';
        }
    }
    $smarty->assign('data', $params['data']);
    $smarty->display('login.html');
}
//
function exec_login(&$params, &$smarty, &$sys)
{
	
    $err = array();
    if ($params['data']['email'] != '' && $params['data']['login_pass'] != ''){
    	$reurl = "../menu.php";
        if (!$sys->login($params['data']['email'], $params['data']['login_pass'] , $reurl )){
            $err['Invalid'] = true;
        }
    }
    else{
        $err['Blank'] = true;
    }
    $smarty->assign('err', $err);
    return 'login';
}
//
function exec_get_site_list(&$params, &$smarty, &$sys)
{
    $site_list = $sys->get_staff_site_list();
    echo('<ul>');
    foreach($site_list as $site){
        echo('<li><a href="javascript:;" id="'. $site['id'] .'"><span><img src="/site_'. sprintf('%04d', $site_id) .'/images/'. $site['logo_file'] .'"/></span>'. htmlspecialchars($site['site_name'], ENT_QUOTES) .'</a></li>');
    }
    echo('</ul>');
    exit();    
}
function exec_change_site(&$params, &$smarty, &$sys)
{
    $site_id = $params['site_id'];
    echo( $sys->change_site($site_id) );
    exit();
}
/*

// テンポラリディレクトリクリア
function temp_clear( $temp_dir ){
	
	$delete_time = 60 * 60 * 24 * 2;	// 48h経ってるファイルを削除
	
	$res = opendir( $temp_dir );

	while( $fname = readdir( $res ) ){

		if( !is_dir( $temp_dir . $fname ) ){
			if( filemtime( $temp_dir . $fname ) < time() - $delete_time ){
				unlink( $temp_dir . $fname );
			}
		}
	}
	closedir( $res );

}
*/
?>